import os
import time

import pytest

from common.readData import readData
from common.Assert import Assert
from common.DButils import select,back_up,rollback
from common import loginOperater
from selenium import webdriver
from common.comDB import comDB
from common.serviceWeb import ServiceWeb
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

com_db=comDB()
read_data=readData()

class TestLoan():
    @pytest.mark.parametrize("username,password,amount,term,expect", read_data.read_data(8, 5))
    def test_loan(self, username,password,amount,term,expect):
        service = ServiceWeb()

        backup = []

        try:
            # 拉起浏览器
            chromeDriver = service.get_driver("http://localhost:90/")
            # 链接数据库
            con, cursor = com_db.get_con()
            # 数据库备份
            backup = back_up("loan")

            # 登录
            login = loginOperater.LoginOperater()
            login.login(chromeDriver, username, password)
            time.sleep(1)

            sql1 = "select * from loan"
            result1 = len(select(sql1,()))

            #进入安全网贷界
            chromeDriver.get("http://localhost:90/user/tools/toApplyLoan.html")
            chromeDriver.maximize_window()

            # 初始化显式等待对象
            wait = WebDriverWait(chromeDriver, 10)

            el4=wait.until(
                EC.visibility_of_element_located((By.ID,"amount"))
            )
            el4.send_keys(amount)
            el5=wait.until(
                EC.visibility_of_element_located((By.ID,"term"))
            )
            el5.send_keys(term)
            el6=wait.until(
                EC.element_to_be_clickable((By.ID,"submit"))
            )
            el6.click()


            el7=wait.until(
                EC.element_to_be_clickable((By.XPATH,"/html/body/div[2]/div[2]/div/div/div/div/div/div/div/div[4]/button[1]"))
            )
            el7.click()

            """#登录管理员审核
            chromeDriver.get("http://localhost:90")
            # 窗口最大化
            chromeDriver.maximize_window()
            chromeDriver.find_element(By.ID, "username").send_keys("admin")
            chromeDriver.find_element(By.ID, "password").send_keys("123456")
            time.sleep(2)
            chromeDriver.find_element(By.ID, "login_btn").click()
            time.sleep(2)
            chromeDriver.get("http://localhost:90/admin/loan/toLoanexam.html?pageNum=1&pageSize=5")
            chromeDriver.maximize_window()
            time.sleep(2)
            #审核通过
            chromeDriver.find_element(By.XPATH,"/html/body/div/div/main/div/div/div/div/div[2]/div/table/tbody/tr/td[7]/button[1]").click()
            time.sleep(2)
            chromeDriver.find_element(By.XPATH,"/html/body/div[2]/div[2]/div/div/div/div/div/div/div/div[4]/button[1]").click()
            time.sleep(2)"""


            #查询结果
            result2 = len(select(sql1,()))
            result=result2-result1

            # 断言
            screenshot_dir = "D:\pythonproject\自动化_1封装\失败截图\loan"
            screenshot_path = f"{screenshot_dir}/{username}失败截图.png"
            Assert(chromeDriver, result, expect, screenshot_path)

        finally:
            # 数据库恢复
            rollback(backup, "loan")
            # 资源清理
            cursor.close()
            con.close()
            chromeDriver.quit()

            chromeDriver.quit()









